using UnityEngine;
using QFramework;
using System.Collections.Generic;

namespace Projectsurviva
{
	public partial class SimpleRoteSword : ViewController
	{
        List<Collider2D> objlist=new List<Collider2D>();
        public int defaultatk = 2;
        void Start()
        {
          
            Circle.Hide();
            void CreatSword()
            {
                objlist.Add(Circle.InstantiateWithParent(this).Show()
                       .Self(self =>
                       {
                           self.OnTriggerEnter2DEvent(collieder =>
                           {
                               var hurtbox = collieder.GetComponent<HurtBoxenemy>();
                               if (hurtbox != null)
                               {
                                   if (hurtbox.Owener.CompareTag("Enemy"))
                                   {
                                       DamageSystem.CalculateDamage(Global.SimpleSwordDuration.Value, hurtbox.Owener.GetComponent<Enemy>());

                                       // hurtbox.Owener.GetComponent<Enemy>()?.Hurt(defaultatk);

                                       if (UnityEngine.Random.Range(0, 1.0f) < 0.5f)
                                       {
                                           collieder.attachedRigidbody.velocity = collieder.NormalizedDirection2DFrom(self) * 5
                                           + collieder.NormalizedDirection2DFrom(Play.Default) * 10;
                                       }

                                   }
                               }

                           }).UnRegisterWhenGameObjectDestroyed(self);

                       }));
            }
            void  CreatSwords()
            {
                var toAddCount = Global.SimpleRotateSwordCount.Value + Global.AdditionalFlyThinigCount.Value - objlist.Count;

                for (global::System.Int32 i = 0; i < toAddCount; i++)
                {

                    CreatSword();
                }
                UpdataCirclePos();
            }
            Global.SimpleRotateSwordCount.Or(Global.AdditionalFlyThinigCount)  .Register(() =>
            {

                CreatSwords();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.SimpleRotateSwordRange.Register(range=> UpdataCirclePos()).UnRegisterWhenGameObjectDestroyed(gameObject);
            CreatSwords();
        }
        void UpdataCirclePos()
        {
            var radius = Global.SimpleRotateSwordRange.Value ;
            var durationDegrees = 360 / objlist.Count;

            for (global::System.Int32 i = 0; i < objlist.Count; i++)
            {
            var circlelocalpos = new Vector2(Mathf.Cos(durationDegrees*i * Mathf.Deg2Rad), Mathf.Sin(durationDegrees*i * Mathf.Deg2Rad)) * radius;
                //  var circlelocalpos = new Vector2(Mathf.Cos(0 * Mathf.Deg2Rad), Mathf.Sin(0 * Mathf.Deg2Rad) * radius);

                objlist[i].LocalPosition(circlelocalpos.x, circlelocalpos.y).LocalEulerAnglesZ(durationDegrees*i - 90);
                
            }
        }
        private void Update()
        {
          
            var degree = Time.frameCount*Global.SimpleRotateSwordSpeed.Value;
            this.LocalEulerAnglesZ(-degree);

          

        }
    }
}
